package com.microfch.p;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.microfch.m.DataCenter;
import com.microfch.ninetypetest.MainActivity;
import com.microfch.ninetypetest.R;
import com.microfch.v.QuestionAdpter;
import com.microfch.v.ResultActivity;

import java.util.HashMap;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MainActivityP implements View.OnClickListener {
    private static MainActivityP sInstance;
    public final String TAG = this.getClass().getSimpleName();
    DataCenter m=null;
    private Context mContext;
    SoundPool soundPool = new SoundPool(2,AudioManager.STREAM_RING,0);
    HashMap musicId=new HashMap();

    public static MainActivityP getInstance() {
        if (sInstance == null) {
            sInstance = new MainActivityP();
        }
        return sInstance;

    }


    public void playChose(){
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                soundPool.play((Integer) musicId.get(1),1,1,0,0,1);

            }
        },20);
    }
    public  void playSubmit(){
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                soundPool.play((Integer) musicId.get(2),1,1,0,0,1);
            }
        },20);

    }

    public void loadSuggestion(){
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                m.loadSuggestion();
            }
        },500);

    }

    public void initView() {
        initData();
        musicId.put(1,soundPool.load(mContext,R.raw.b,1));
        musicId.put(2,soundPool.load(mContext,R.raw.o,1));
        Log.d(TAG,"initView");
    }
    private void initData() {
        if(m== null) {
            m = DataCenter.getInstance();
            m.setContext(mContext);
            m.loadData();
            Log.d(TAG, "loadData");
            m.initQuestion(m.RANDOM);
            loadSuggestion();

        }
    }

    public void setContext(Context context) {
        mContext = context;
    }

    public void initRecyclerView(RecyclerView recyclerView) {
        QuestionAdpter questionAdpter = new QuestionAdpter(m.getQuestionList());
        recyclerView.setAdapter(questionAdpter);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()== R.id.submit_button){
            String res = m.getResult();
          //  Toast.makeText(mContext,res,Toast.LENGTH_LONG).show();
            Intent intent = new Intent(mContext,ResultActivity.class);
            intent.putExtra("res",res);
            mContext.startActivity(intent);
            MainActivity activity =   (MainActivity)mContext;
            activity.overridePendingTransition(R.anim.move_right_in_activity, R.anim.move_left_out_activity);
            playSubmit();
        }
    }
}
